<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <!-- Logo + 标题 -->
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/oj-logo.svg" />
            <div class="title">CodeRush</div>
          </div>
        </a-menu-item>

        <!-- 动态菜单 -->
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>

        <!-- 我的 -->
        <a-menu-item key="my" @click="goMy"> 登录 </a-menu-item>
      </a-menu>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";

const router = useRouter();
const store = useStore();

// 用户信息
const loginUser = computed(() => store.state.user?.loginUser ?? {});

// 筛选显示的菜单项
const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) return false;
    return checkAccess(loginUser.value, item.meta?.access as string);
  });
});

// 当前选中菜单项
const selectedKeys = ref(["/"]);
router.afterEach((to) => {
  selectedKeys.value = [to.path];
});

// 菜单点击跳转
const doMenuClick = (key: string) => {
  router.push({ path: key });
};

// 我的：登录判断跳转
const goMy = () => {
  router.push("/user/login");
};
const logout = () => {
  store.commit("user/clearUser");
  router.push("/user/login");
};
// 退出登录逻辑
// const logout = () => {
//   store.commit("user/setLoginUser", {}); // 清空用户信息（根据你的 store 模块改）
//   router.push("/user/login");
// };
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
  font-size: 20px;
  font-weight: bold;
}

.logo {
  height: 48px;
}
</style>
